package alarm.ruiqi.com.alarm_pro.service;

import android.content.Context;

import com.ab.util.AbFileUtil;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;

import alarm.ruiqi.com.alarm_pro.R;

/**
 * Created by 徐宇健 on 2015/8/25 0025.
 * // FIXME: 2015/8/25 0025  需要加入设置
 */
public class SpeechServiceImpl {


    private Context mContext;
    private SpeechSynthesizer mTts;
    private static SpeechServiceImpl speechService;
    private String  mVoiceName = "";

    public static SpeechServiceImpl getInstance() {
        if (speechService == null) {
            speechService = new SpeechServiceImpl();
        }
        return speechService;
    }

    private SpeechServiceImpl() {
        mVoiceName = "xiaoyan";
        mContext = MobileApplication.getInstance();
        //1.创建 SpeechSynthesizer 对象,  第二个参数：本地合成时传 InitListener
        mTts = SpeechSynthesizer.createSynthesizer(mContext, null);
        //2.合成参数设置，详见《科大讯飞MSC API手册(Android)》SpeechSynthesizer  类
        //设置发音人（更多在线发音人，用户可参见  附录12.2

        mTts.setParameter(SpeechConstant.SPEED, "50");//设置语速
        mTts.setParameter(SpeechConstant.VOLUME, "80");//设置音量，范围 0~100
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端
        //设置播放器音频流类型
        mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");
        // 设置播放合成音频打断音乐播放，默认为true
        mTts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");
        mTts.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, AbFileUtil.getFileDownloadDir(mContext));

    }

    public String[] getCloudVoicersValue() {
        return mContext.getResources().getStringArray(R.array.voicer_cloud_entries);
    }

    public String[] getCloudVoicersEntries() {
        return mContext.getResources().getStringArray(R.array.voicer_cloud_values);
    }

    public void setVoiceName(String mVoiceName) {
        this.mVoiceName = mVoiceName;
    }

    public  void setVoiceName(){
        mTts.setParameter(SpeechConstant.VOICE_NAME,mVoiceName );
    }


    public int startSpeaking(String object, SynthesizerListener mTtsListener) {
        setVoiceName();
        return mTts.startSpeaking(object,mTtsListener);

    }

    public  void stopSpeeking(){
        mTts.stopSpeaking();
        mTts.destroy();
    }

    public  boolean isSpeeking(){
        return  mTts.isSpeaking();
    }

    public  void pauseSpeeking() {
        mTts.pauseSpeaking();
    }

    public void resumeSpeeking() {
        mTts.resumeSpeaking();
    }

}
